package cn.edu.wynu.oop.polymorphic;

abstract class Income {
    protected double amount; // 复用属性
    public Income(double amount) { this.amount = amount; } // 复用构造方法
    public abstract double calculateTax(); // 统一计税方法（子类实现细节）
}

// 2. 子类：专注实现各自计税逻辑（无重复代码）
class SalaryIncome extends Income {
    public SalaryIncome(double amount) { super(amount); }
    @Override
    public double calculateTax() { // 工资计税逻辑
        double taxable = amount - 5000;
        return taxable > 0 ? taxable * 0.03 : 0;
    }
}

class BonusIncome extends Income {
    public BonusIncome(double amount) { super(amount); }
    @Override
    public double calculateTax() { // 奖金计税逻辑
        return amount * 0.1;
    }
}

class FreelanceIncome extends Income {
    public FreelanceIncome(double amount) { super(amount); }
    @Override
    public double calculateTax() { // 兼职计税逻辑
        double taxable = amount - 800;
        return taxable > 0 ? taxable * 0.2 : 0;
    }
}
public class WithPolymorphismExample {
    public static void main(String[] args) {
        Income[] incomes = {new SalaryIncome(10000),
                new BonusIncome(5000),
                new FreelanceIncome(3000)};

        double totalTax = 0;
        for (Income income : incomes) {
            totalTax += income.calculateTax();
        }
        System.out.println("总税额: " + totalTax);
    }
}
